package com.ikun.common.message;

import com.ikun.common.exception.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @ClassName SendEmail  //类名称
 * @Description: 发送邮件
 * @Author: ximeng    //作者
 * @CreateDate: 2024/1/2 23:34	//创建时间
 * @UpdateUser: 更新人
 * @UpdateDate: 2024/1/2 23:34	//更新时间
 * @UpdateRemark: 更新的信息
 * @Version: 1.0    //版本号
 */

@Component
public class SendEmail {

    //注册邮件文字模板
    public static final String REGISTER_EMAIL_TEMPLATE_SUBJECT = "尊敬的%s,恭喜你！成功在%s上注册账号!";
    public static final String REGISTER_EMAIL_TEMPLATE_CONTENT = "您的：\n账号是：%s,\n密码是：%s,\n请尽快登录系统核对信息并修改密码";

    private final JavaMailSender mailSender;


    private final String senderEmail;

    @Autowired
    public SendEmail(JavaMailSender mailSender, @Value("${spring.mail.username}") String senderEmail) {
        this.mailSender = mailSender;
        this.senderEmail = senderEmail;
    }


    /**
     * 发送纯文本邮件
     * @param to
     * @param subject
     * @param content
     */
    public void sendTextEmail(String to, String subject, String content) {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(message, false, "utf-8");
            helper.setFrom(senderEmail);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, false);
        } catch (MessagingException e) {
            throw new MyException("邮件发送失败");
        }
        mailSender.send(message);
    }

    /**
     * 发送html邮件
     * @param to
     * @param subject
     * @param htmlContent
     */
    public void sendHtmlEmail(String to, String subject, String htmlContent) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8");
            helper.setFrom(senderEmail);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(htmlContent, true);
            mailSender.send(message);
        } catch (MessagingException e) {
            throw new MyException("邮件发送失败");
        }

    }

    /**
     * 发送带文件的邮件
     * @param to
     * @param subject
     * @param text
     * @param filePath
     * @throws MessagingException
     */
    public void sendEmailWithAttachment(String to, String subject, String text, String filePath) throws MessagingException {

        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8");
            helper.setFrom(senderEmail);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text, false);

            FileSystemResource file = new FileSystemResource(new File(filePath));
            helper.addAttachment(file.getFilename(), file);

            mailSender.send(message);
        } catch (MessagingException e) {
            throw new MyException("邮件发送失败");

        }
    }
}


